package org.darcy.controller.admin.user;

import java.util.List;

import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.darcy.config.shiro.ShiroService;
import org.darcy.entity.SysRole;
import org.darcy.framework.annotation.BussinessLog;
import org.darcy.framework.blog.business.enums.ResponseStatus;
import org.darcy.framework.model.PageResult;
import org.darcy.framework.model.ResponseVO;
import org.darcy.framework.util.ResultUtil;
import org.darcy.model.SysRoleModel;
import org.darcy.service.SysRoleResourcesService;
import org.darcy.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import cn.hutool.core.util.StrUtil;

/**
 * 系统角色管理
 */
@RestController
@RequestMapping("/roles")
public class RoleController {
	
	@Autowired
	private SysRoleService roleService;
	@Autowired
	private SysRoleResourcesService roleResourcesService;
	@Autowired
	private ShiroService shiroService;

	@RequiresPermissions("roles")
	@PostMapping("/list")
	public PageResult list(SysRoleModel param) {
		Page p = new Page(param.getP(), param.getPageSize());
		QueryWrapper<SysRole> q = new QueryWrapper<SysRole>();
		if (StrUtil.isNotEmpty(param.getName()))
			q.like("name", param.getName());
		IPage pageInfo = roleService.getPage(p, q);
		return ResultUtil.tablePage(pageInfo);
	}

	@RequiresPermissions("user:allotRole")
	@PostMapping("/rolesWithSelected")
	public ResponseVO<List<SysRole>> rolesWithSelected(Long uid) {
		return ResultUtil.success(null, roleService.queryRoleListWithSelected(uid));
	}

	@RequiresPermissions("role:allotResource")
	@PostMapping("/saveRoleResources")
	@BussinessLog("分配角色拥有的资源")
	public ResponseVO saveRoleResources(Long roleId, String resourcesId) {
		if (StringUtils.isEmpty(roleId)) {
			return ResultUtil.error("error");
		}
		roleResourcesService.addRoleResources(roleId, resourcesId);
		// 重新加载所有拥有roleId的用户的权限信息
		shiroService.reloadAuthorizingByRoleId(roleId);
		return ResultUtil.success("成功");
	}

	@RequiresPermissions("role:add")
	@PostMapping(value = "/add")
	@BussinessLog("添加角色")
	public ResponseVO add(SysRole role) {
		roleService.insert(role);
		return ResultUtil.success("成功");
	}

	@RequiresPermissions(value = { "role:batchDelete", "role:delete" }, logical = Logical.OR)
	@PostMapping(value = "/remove")
	@BussinessLog("删除角色")
	public ResponseVO remove(Long[] ids) {
		if (null == ids) {
			return ResultUtil.error(500, "请至少选择一条记录");
		}
		for (Long id : ids) {
			roleService.delete(id);
			roleResourcesService.removeByRoleId(id);
		}
		return ResultUtil.success("成功删除 [" + ids.length + "] 个角色");
	}

	@RequiresPermissions("role:get")
	@PostMapping("/get/{id}")
	@BussinessLog("获取角色详情")
	public ResponseVO get(@PathVariable Long id) {
		return ResultUtil.success(null, this.roleService.getById(id));
	}

	@RequiresPermissions("role:edit")
	@PostMapping("/edit")
	@BussinessLog("编辑角色")
	public ResponseVO edit(SysRole role) {
		try {
			roleService.update(role);
		} catch (Exception e) {
			e.printStackTrace();
			return ResultUtil.error("角色修改失败！");
		}
		return ResultUtil.success(ResponseStatus.SUCCESS);
	}

}
